package cn.com.dhcc.turbo.service.exch;

import java.io.File;



import cn.com.dhcc.app.core.handler.compresshandler.CompressFilter;
import cn.com.dhcc.app.core.util.FileUtil;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.pub.core.consts.AppConst.CrossNetType;
import cn.com.dhcc.app.pub.core.consts.AppConst.ExchDataType;
import cn.com.dhcc.app.pub.core.consts.KernelParam;
/**
 * 压缩过滤器
 * @author qiao
 * @2015-2-5 @下午3:32:28
 */
public class TurboCompressFilter<T> implements CompressFilter<T>{

	private ExchDataType exchDataType;
	private CrossNetType crossNetType;
	
	public ExchDataType getExchDataType() {
		return exchDataType;
	}

	public CrossNetType getCrossNetType() {
		return crossNetType;
	}

	public TurboCompressFilter(ExchDataType exchDataType,
			CrossNetType crossNetType) {
		this.exchDataType = exchDataType;
		this.crossNetType = crossNetType;
	}

	/**
	 * 是否不压缩
	 */
	@Override
	public boolean exclude(T obj) {
		//如果关闭了压缩开关，则不压缩
		if(KernelParam.compressEnable==false){
			return true;
		}
		//结构化统一压缩
		if(exchDataType==ExchDataType.CM_DATA){//结构化统一压缩
			return false;
		}
		//非结构化
		if(exchDataType==ExchDataType.FILE_DATA){
			long compressMaxSize =  KernelParam.compressMaxSize;
			File file = (File)obj;
			//不在指定文件名内的不压缩
			String compressFileSudfix = KernelParam.compressFileSudfix;
			if(StrUtil.isAllNotBlank(compressFileSudfix)){
				String fileSudfix = FileUtil.getFileSuffix(file);
				String[] compressFileSudfix_toCompress = compressFileSudfix.split("\\|");
				for (int i = 0; i < compressFileSudfix_toCompress.length; i++) {
					if(compressFileSudfix_toCompress[i].equalsIgnoreCase(fileSudfix)){
						//超过规定大小，不压缩
						if(compressMaxSize<file.length()/1024/1024){
							return true;
						}else{
							return false;//压缩
						}
					}
				}
			}
		}
		return true;
	}
}
